class HealthCheckItem:
    """体检项目类"""
    
    def __init__(self, name, description=""):
        self.name = name  # 项目名称
        self.description = description  # 项目描述
        self.result = ""  # 检查结果
        self.reference = ""  # 参考值
    
    def __str__(self):
        return self.name


class HealthCheckPackage:
    """体检套餐类"""
    
    def __init__(self, name, description=""):
        self.name = name  # 套餐名称
        self.description = description  # 套餐描述
        self.items = []  # 套餐包含的体检项目列表
    
    def add_item(self, item):
        """添加体检项目到套餐中"""
        if isinstance(item, HealthCheckItem):
            self.items.append(item)
            return True
        return False
    
    def remove_item(self, item_name):
        """从套餐中移除体检项目"""
        for item in self.items:
            if item.name == item_name:
                self.items.remove(item)
                return True
        return False
    
    def get_item(self, item_name):
        """获取套餐中的特定项目"""
        for item in self.items:
            if item.name == item_name:
                return item
        return None
    
    def __str__(self):
        return f"{self.name} ({len(self.items)}项)"


class HealthCheckSystem:
    """体检系统数据管理类"""
    
    def __init__(self):
        self.packages = []  # 所有体检套餐
        self.current_patient = None  # 当前患者信息
    
    def add_package(self, package):
        """添加体检套餐"""
        if isinstance(package, HealthCheckPackage):
            self.packages.append(package)
            return True
        return False
    
    def remove_package(self, package_name):
        """移除体检套餐"""
        for package in self.packages:
            if package.name == package_name:
                self.packages.remove(package)
                return True
        return False
    
    def get_package(self, package_name):
        """获取特定体检套餐"""
        for package in self.packages:
            if package.name == package_name:
                return package
        return None
    
    def set_patient_info(self, patient_info):
        """设置当前患者信息"""
        self.current_patient = patient_info
    
    def save_to_file(self, filename):
        """将所有套餐信息保存到文件"""
        import json
        import os
        
        data = {
            "packages": []
        }
        
        for package in self.packages:
            package_data = {
                "name": package.name,
                "description": package.description,
                "items": []
            }
            
            for item in package.items:
                item_data = {
                    "name": item.name,
                    "description": item.description,
                    "reference": item.reference
                }
                package_data["items"].append(item_data)
            
            data["packages"].append(package_data)
        
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        
        return os.path.exists(filename)
    
    def load_from_file(self, filename):
        """从文件加载套餐信息"""
        import json
        import os
        
        if not os.path.exists(filename):
            return False
        
        try:
            with open(filename, 'r', encoding='utf-8') as f:
                data = json.load(f)
            
            # 清除现有套餐
            self.packages = []
            
            # 加载套餐
            for package_data in data.get("packages", []):
                package = HealthCheckPackage(
                    package_data.get("name", "未命名套餐"),
                    package_data.get("description", "")
                )
                
                # 加载项目
                for item_data in package_data.get("items", []):
                    item = HealthCheckItem(
                        item_data.get("name", "未命名项目"),
                        item_data.get("description", "")
                    )
                    item.reference = item_data.get("reference", "")
                    package.add_item(item)
                
                self.add_package(package)
            
            return True
            
        except Exception as e:
            print(f"加载文件失败: {str(e)}")
            return False
    
    # 创建一些预设的体检套餐
    def create_default_packages(self):
        """创建默认体检套餐"""
        # 基础体检套餐
        basic_package = HealthCheckPackage("基础体检", "基础身体检查套餐")
        basic_package.add_item(HealthCheckItem("心电图", "检查心脏电活动"))
        basic_package.add_item(HealthCheckItem("内科检查", "常规内科检查"))
        basic_package.add_item(HealthCheckItem("血压测量", "测量收缩压和舒张压"))
        basic_package.add_item(HealthCheckItem("身高体重", "测量身高和体重"))
        self.add_package(basic_package)
        
        # 高级体检套餐
        advanced_package = HealthCheckPackage("高级体检", "全面体检套餐")
        advanced_package.add_item(HealthCheckItem("心电图", "检查心脏电活动"))
        advanced_package.add_item(HealthCheckItem("内科检查", "常规内科检查"))
        advanced_package.add_item(HealthCheckItem("血常规", "完整血液检查"))
        advanced_package.add_item(HealthCheckItem("B超检查", "腹部B超检查"))
        advanced_package.add_item(HealthCheckItem("血压测量", "测量收缩压和舒张压"))
        advanced_package.add_item(HealthCheckItem("肝功能", "肝脏功能指标检查"))
        self.add_package(advanced_package)
        
        # 老年人体检套餐
        elder_package = HealthCheckPackage("老年体检", "针对老年人的体检套餐")
        elder_package.add_item(HealthCheckItem("心电图", "检查心脏电活动"))
        elder_package.add_item(HealthCheckItem("内科检查", "常规内科检查"))
        elder_package.add_item(HealthCheckItem("血糖测量", "空腹血糖检测"))
        elder_package.add_item(HealthCheckItem("骨密度测定", "骨质密度检查"))
        elder_package.add_item(HealthCheckItem("血压测量", "测量收缩压和舒张压"))
        self.add_package(elder_package)
        
        return True 